// 仪表盘页面JS模块
layui.define(['layer', 'jquery'], function(exports) {
    var layer = layui.layer;
    var $ = layui.jquery;
    
    var dashboard = {
        // 初始化仪表盘
        init: function() {
            this.loadStats();
            this.loadPendingTasks();
            this.loadUpcomingDeadlines();
        },
        
        // 加载统计数据
        loadStats: function() {
            $.ajax({
                url: '/devc/api/dashboard/stats',
                type: 'GET',
                success: function(res) {
                    if (res.code === 0) {
                        var data = res.data;
                        $('.stat-value').eq(0).text(data.pendingRequirements || 0);
                        $('.stat-value').eq(1).text(data.inProgressApplications || 0);
                        $('.stat-value').eq(2).text(data.developingVersions || 0);
                        $('.stat-value').eq(3).text(data.pendingTasks || 0);
                    } else {
                        layer.msg(res.msg || '获取统计数据失败');
                    }
                },
                error: function() {
                    layer.msg('网络错误，请检查连接');
                }
            });
        },
        
        // 加载待处理任务列表
        loadPendingTasks: function() {
            $.ajax({
                url: '/devc/api/dashboard/pending-tasks',
                type: 'GET',
                success: function(res) {
                    if (res.code === 0) {
                        var tasks = res.data || [];
                        var tbody = $('.layui-card:eq(1) tbody');
                        tbody.empty();
                        
                        if (tasks.length === 0) {
                            tbody.append('<tr><td colspan="4">暂无数据</td></tr>');
                        } else {
                            tasks.forEach(function(task) {
                                var statusHtml = dashboard.getStatusHtml(task.status);
                                tbody.append('<tr>' +
                                    '<td>' + task.name + '</td>' +
                                    '<td>' + (task.featureName || '') + '</td>' +
                                    '<td>' + (task.versionNumber || '') + '</td>' +
                                    '<td>' + statusHtml + '</td>' +
                                    '</tr>');
                            });
                        }
                    } else {
                        layer.msg(res.msg || '获取待处理任务失败');
                    }
                },
                error: function() {
                    layer.msg('网络错误，请检查连接');
                }
            });
        },
        
        // 加载近期截止任务提醒
        loadUpcomingDeadlines: function() {
            $.ajax({
                url: '/devc/api/dashboard/upcoming-deadlines',
                type: 'GET',
                success: function(res) {
                    if (res.code === 0) {
                        var tasks = res.data || [];
                        var tbody = $('.layui-card:eq(2) tbody');
                        tbody.empty();
                        
                        if (tasks.length === 0) {
                            tbody.append('<tr><td colspan="4">暂无数据</td></tr>');
                        } else {
                            tasks.forEach(function(task) {
                                var statusHtml = dashboard.getStatusHtml(task.status);
                                tbody.append('<tr>' +
                                    '<td>' + task.name + '</td>' +
                                    '<td>' + (task.featureName || '') + '</td>' +
                                    '<td>' + (task.versionNumber || '') + '</td>' +
                                    '<td>' + statusHtml + '</td>' +
                                    '</tr>');
                            });
                        }
                    } else {
                        layer.msg(res.msg || '获取近期任务失败');
                    }
                },
                error: function() {
                    layer.msg('网络错误，请检查连接');
                }
            });
        },
        
        // 获取状态HTML
        getStatusHtml: function(status) {
            var badgeClass = 'layui-badge';
            switch(status) {
                case '待处理':
                    badgeClass += ' layui-bg-orange';
                    break;
                case '进行中':
                    badgeClass += ' layui-bg-blue';
                    break;
                case '已完成':
                    badgeClass += ' layui-bg-green';
                    break;
                case '已取消':
                    badgeClass += ' layui-bg-gray';
                    break;
                default:
                    badgeClass += ' layui-bg-gray';
            }
            return '<span class="' + badgeClass + '">' + status + '</span>';
        }
    };
    
    exports('dashboard', dashboard);
});